﻿<%@ WebHandler Language="C#" Class="Assesment.Web.Ajaxhandler.Administration.UserManagement.sMenuRightshandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using Assesment.Library;

using Assesment.Library.DataAccess.Administration;
using Assesment.Library.DomainObject.Administration;

using Assesment.Library.DataAccess.Administration.UserManagement;
using Assesment.Library.DomainObject.Administration.UserManagement;
using Assesment.Library.Enum;

using Assesment.Web.Support;

namespace Assesment.Web.Ajaxhandler.Administration.UserManagement
{
    public class sMenuRightshandler : BaseHandler
    {

        private recMenuRightsRepo menuRightsDal = new recMenuRightsRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

        private string MethodName = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                case ("insert"):
                    context.Response.Write(Insert(context));
                    break;
                case ("getdatabyroleid"):
                    context.Response.Write(GetDataByRoleId(context));
                    break;

                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler

        private string Insert(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();

            try
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
                {
                    string line = string.Empty;
                    line = context.Server.UrlDecode(sr.ReadToEnd());

                    var menu_rights = Newtonsoft.Json.JsonConvert.DeserializeObject<List<recMenuRights>>(line);

                    menu_rights.ForEach(delegate(recMenuRights obj)
                    {
                        obj.CreatedBy = base.UserLoggedIn;
                        obj.CreatedDate = DateTime.Now;
                    });

                    menuRightsDal.SaveDelete(menu_rights);

                    helper.IsSuccess = true;
                    helper.Message = "Data berhasil di simpan !";
                }
            }
            catch (Exception ex)
            {
                helper.IsSuccess = false;
                helper.Message = ex.Message;
            }
            finally
            {

                recLog log = new recLog();
                log.LogEvent = "Insert";
                log.LogMessage = helper.Message;
                log.LogSource = "sMenuRightshandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }




            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);

        }

        public string GetDataByRoleId(HttpContext context)
        {
            base.ProcessRequest(context);

            int roleId = 0;
            if (context.Request.Params["roleId"] != null && context.Request.Params["roleId"].ToString().Trim()!="")
                roleId = Convert.ToInt32(context.Request.Params["roleId"]);

            List<recMenuRights> list = menuRightsDal.GetList();
            var result = list.Where(x => x.RoleID == roleId);

            return Newtonsoft.Json.JsonConvert.SerializeObject(result);

        }

        #endregion

    }
}